package Model
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind;
	import mx.events.PropertyChangeEvent;
	
	import Controller.OverviewPaletteController;
	import Controller.TransparentComponentController;

	[Bindable]
	public class ComplexSolidComponent extends SolidComponent
	{
		private var _totalWallArea:Number = 0;
		private var _orientation:Number;
		private var _windows:ArrayCollection;
		private var _doors:ArrayCollection;
		private static var overviewPalleteController:OverviewPaletteController = new OverviewPaletteController();
		private var transparentController:TransparentComponentController = new TransparentComponentController();

		public function ComplexSolidComponent(description:String, quantity:Number=1, length:Number=0, height:Number=0, area:Number=0, orientation:Number=0)
		{
			super(description, quantity, length, height, area);
			this._doors = new ArrayCollection();
			this._doors.addEventListener(CollectionEvent.COLLECTION_CHANGE, handleDoorsCollectionChange);
			this._windows = new ArrayCollection();
			this._windows.addEventListener(CollectionEvent.COLLECTION_CHANGE, handleWindowsCollectionChange);
		}
		
		public function get totalWallArea():Number
		{
			return _totalWallArea;
		}

		public function set totalWallArea(value:Number):void
		{
			_totalWallArea = value;
			this.area = value;
		}

		override public function set area(value:Number):void
		{
			//this._area = value;
			calculateArea();
		}
		
		
		protected function handleDoorsCollectionChange(event:CollectionEvent):void
		{
			//overviewPalleteController.updateWindowsDoorsSectionData();
			if(event.kind == CollectionEventKind.ADD)
			{
				(FlexGlobals.topLevelApplication as ISOVER_Designer_iPad).project.areas.doors.addItemAt(event.items[0] as TransparentComponent, this.doors.getItemIndex(event.items[0]));
			}			
			else if(event.kind == CollectionEventKind.REMOVE)
			{
				(FlexGlobals.topLevelApplication as ISOVER_Designer_iPad).project.areas.doors.removeItemAt((FlexGlobals.topLevelApplication as ISOVER_Designer_iPad).project.areas.doors.getItemIndex(event.items[0] as TransparentComponent));
			}
			if(event.kind == CollectionEventKind.UPDATE && event.items[0].property == "area")
			{
				var originalArea:Number = _area;
				this.area = 1;
				this.area = originalArea;
			}
			//FlexGlobals.topLevelApplication.resetCalculations();
		}
		
		override protected function calculateArea():void
		{
			// TODO Auto Generated method stub
			super.calculateArea();
			var newEnteredArea:Number = totalWallArea;
			var totalWindowsArea:Number = calculateTotalWindowArea();
			var totalDoorsArea:Number = calculateTotalDoorsArea();
			
			var calculatedArea:Number = newEnteredArea - (totalWindowsArea + totalDoorsArea);
			
			this._area = calculatedArea;
		}
		
		private function calculateTotalWindowArea():Number
		{
			var result:Number = 0;
			
			for each(var window:TransparentComponent in this.windows)
			{
				result += window.area;
			}
			
			return result;
		}
		
		private function calculateTotalDoorsArea():Number
		{
			var result:Number = 0;
			
			for each(var door:TransparentComponent in this.doors)
			{
				result += door.area;
			}
			
			return result;
		}
		
		protected function handleWindowsCollectionChange(event:CollectionEvent):void
		{
			//overviewPalleteController.updateWindowsDoorsSectionData();
			if(event.kind == CollectionEventKind.ADD)
			{
				(FlexGlobals.topLevelApplication as ISOVER_Designer_iPad).project.areas.transparentComponents.addItemAt(event.items[0] as TransparentComponent, this.windows.getItemIndex(event.items[0]));
				calculateArea();
			}			
			else if(event.kind == CollectionEventKind.REMOVE)
			{
				(FlexGlobals.topLevelApplication as ISOVER_Designer_iPad).project.areas.transparentComponents.removeItemAt((FlexGlobals.topLevelApplication as ISOVER_Designer_iPad).project.areas.transparentComponents.getItemIndex(event.items[0] as TransparentComponent));
				calculateArea();
			}
			if(event.kind == CollectionEventKind.UPDATE && event.items[0].property == "area")
			{
				var originalArea:Number = _area;
				this.area = 1;
				this.area = originalArea;
			}
			
			//FlexGlobals.topLevelApplication.resetCalculations();
		}
		
		public function get orientation():Number
		{
			return _orientation;
		}
		
		public function set orientation(value:Number):void
		{
			_orientation = value;
		}

		public function get windows():ArrayCollection
		{
			return _windows;
		}

		public function set windows(value:ArrayCollection):void
		{
			_windows = value;
		}

		public function get doors():ArrayCollection
		{
			return _doors;
		}

		public function set doors(value:ArrayCollection):void
		{
			_doors = value;
		}
		
		public function addWindow(window:TransparentComponent):void
		{
			this._windows.addItem(window);
		}
		
		public function removeWindow(window:TransparentComponent):void
		{
			this._windows.removeItemAt(this._windows.getItemIndex(window));
		}
		
		public function getWindowById(id:String):TransparentComponent
		{
			var result:TransparentComponent;
			
			for each(var window:TransparentComponent in this._windows)
			{
				if(window.id == id)
				{
					result = window;
				}
			}
			
			return result;
		}
		
		public function addDoor(door:TransparentComponent):void
		{
			this._doors.addItem(door);
		}
		
		public function removeDoor(door:TransparentComponent):void
		{
			this._doors.removeItemAt(this._doors.getItemIndex(door));
		}
		
		public function getDoorById(id:String):TransparentComponent
		{
			var result:TransparentComponent;
			for each(var door:TransparentComponent in this._doors)
			{
				if(door.id == id)
				{
					result = door;
				}
			}
			
			return result;
		}
	}
}